<!DOCTYPE html>
<html>
<head>
    <title>视频教程</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .video-card {
            transition: transform 0.2s;
            cursor: pointer;
        }
        .video-card:hover {
            transform: translateY(-5px);
        }
        .video-cover {
            height: 200px;
            object-fit: cover;
        }
        .video-title {
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
    </style>
</head>
<body>
    <div class="container py-4">
        <h1 class="text-center mb-4">视频教程</h1>
        
        <div class="row g-4">
            {% for video in videos %}
            <div class="col-md-4">
                <div class="card h-100 video-card" onclick="playVideo({{ video.id }})">
                    <img src="{{ video.cover_url }}" class="card-img-top video-cover" alt="{{ video.title }}">
                    <div class="card-body">
                        <h5 class="card-title video-title">{{ video.title }}</h5>
                        <p class="card-text text-muted">
                            <small>
                                <span class="badge bg-primary">{{ video.category }}</span>
                                <span class="ms-2"><i class="bi bi-eye"></i> {{ video.views }}</span>
                                <span class="float-end">{{ video.created_at }}</span>
                            </small>
                        </p>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- 视频播放模态框 -->
    <div class="modal fade" id="videoModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="ratio ratio-16x9">
                        <video id="videoPlayer" controls class="w-100">
                            <p>您的浏览器不支持 HTML5 视频播放。</p>
                        </video>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function playVideo(videoId) {
            fetch(`/video/${videoId}/preview`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        const modal = new bootstrap.Modal(document.getElementById('videoModal'));
                        const player = document.getElementById('videoPlayer');
                        
                        document.querySelector('#videoModal .modal-title').textContent = data.title;
                        player.src = data.video_url;
                        player.poster = data.cover_url;
                        
                        modal.show();
                    } else {
                        alert('加载视频失败，请稍后重试');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('系统错误，请稍后重试');
                });
        }

        // 关闭模态框时停止视频播放
        document.getElementById('videoModal').addEventListener('hidden.bs.modal', function () {
            document.getElementById('videoPlayer').pause();
        });
    </script>
</body>
</html> 